const shoppingService = require("../server/shopping");
const { wirteJSON } = require("../utils/func");
const path = require("path");
module.exports = class shoppingController {
  //获取个人购物车列表
  static async getShoppingData(ctx) {
    try {
      const { uid, page } = ctx.request.body;
      const tableData = await shoppingService.getShoppingData(uid, page);
      const total = await shoppingService.getSum(uid);
      wirteJSON(ctx, 200, "获取个人购物车数据成功！！", { tableData, total });
    } catch (err) {
      console.log(err);
      wirteJSON(ctx, 500, err.message, null);
    }
  }

  // 添加数据
  static async addShopping(ctx) {
    try {
      const { shopping } = ctx.request.body;
      const data = JSON.parse(shopping);
      const res = await shoppingService.addShopping(data);
      if (res.affectedRows) {
        wirteJSON(ctx, 200, "数据插入成功！！", "数据插入成功！！");
      } else {
        wirteJSON(ctx, 404, "数据插入失败！！", "数据插入失败！！");
      }
    } catch (err) {
      wirteJSON(ctx, 500, err.message, null);
    }
  }

  // 删除数据
  static async batchDeteleShopping(ctx) {
    try {
      const { shopping } = ctx.request.body;
      const data = JSON.parse(shopping);
      const res = await shoppingService.batchDeteleShopping(data);
      if (res) {
        wirteJSON(ctx, 200, "数据删除成功！！", "数据删除成功！！");
      } else {
        wirteJSON(ctx, 404, "数据删除失败", "数据删除失败");
      }
    } catch (err) {
      wirteJSON(ctx, 500, err.message, null);
    }
  }
};
